/**
 * Created by tongjunshui on 17/6/9.
 */
var express = require('express');
var router = express.Router();

var moment = require('moment');
var dbDao = require('../../models/dbBase.js');
var zdbDao=require('../../models/zdbBase.js');
const members = new zdbDao("members");


router.get('/wechat/:wxopenid', function (req,res){
            var wxopenid = req.params.wxopenid;
            if (!wxopenid) {

                res.send({result:'error',message:'无效的openid'});
            } else {
                var sql = "select * from members where wxopenId='" + wxopenid + "'";
                dbDao.find(sql, function (err, result2) {
                    if(err){
                        res.send({result:'error',message:'查询用户失败'});
                    }else{
                        if (result2.length > 0) {
                            console.log("老微信一键登陆用户", result2[0]);

                            res.send({result:'success',
                                data: result2[0]
                            });
                        }else{
                            res.send({result:'error',message:'没有查询到用户'});
                        }
                    }
                });
            }
});
router.get('/phone',function(req, res){
    var data ={
        wxopenId: req.query.wxopenId || ''
    }
    members.findByColumn(data,function(err, result){
        var phone ='';
        if(err){
            res.send({result:'error',message:'查询失败'});
        }else{
            if(result.length>0){
                phone = result[0].phone;
                res.send({result:'success',data:{phone:phone}});
            }else{
                res.send({result:'error',message:'未找到电话号码'});
            }
        }
    })

})

router.get('/verifyPin',function(req, res){
    const {phone, pin} = req.query;
    if(!phone){
        return res.send({result:false,message:'缺少phone参数'});
    }
    if(!pin){
        return res.send({result:false,message:'缺少pin参数'});
    }
    var sql = "select * from smsPinVerify where phone='" +
        phone +"' and pin ='"+pin +
        "' and created > " + (moment().unix()-60);
    dbDao.find(sql, function (err, result) {
        if(result && result.length >= 1 ){
            res.send({
                result:true
            });
        }else{
            res.send({
                result:false,message:'短信验证失败'
            });
        }
    })
})
router.get('/updatePhone',function(req, res){
    var obj ={
        wxopenId: req.query.wxopenId || ""
    };
    var data = {
        phone:req.query.phone ||""
    };
    var pin=req.query.pin;

    if((!obj.wxopenId)||(!data.phone)||(!pin)){
        res.send({
            result:'error',message:'openid或电话号无效'
        });
    }else{
        var sql = "select * from smsPinVerify where phone='" +
            req.query.phone +"' and pin ='"+req.query.pin +
            "' and created > " + (moment().unix()-60);

        dbDao.find(sql,function (err, result2) {
            if(err){
                res.send({result:'error',message:'pin验证失败'});
            }else{
                if (result2.length > 0) {
                    members.updateByColumns( data,obj, function(err, result){
                        //console.log("result",result.affectedRows);
                        if(err){
                            res.send({
                                result:'error',message:'更新失败'
                            });
                        }else{
                            if(result.affectedRows === 1){
                                res.send({
                                    result:"success"
                                });
                            }else{
                                res.send({
                                    result:'error',message:'未找到用户'
                                });
                            }
                        }
                    })
                }else{
                    res.send({result:'error',message:'pin无效'});
                }
            }
        });
    }
})
module.exports = router;